const webpack = require("webpack");
module.exports = {
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/preset-scss",
  ],
  webpackFinal: async (config) => {
    // 处理linaria
    config.module.rules[0].test = /\.(mjs|tsx?|jsx?|js|ts)$/;
    config.module.rules[0].use.push({
      loader: "@linaria/webpack-loader",
      options: {
        babelOptions: {
          presets: [
            require.resolve("@babel/preset-env"),
            require.resolve("@babel/preset-typescript"),
            require.resolve("@linaria/babel-preset"),
          ],
        },
      },
    });

    // 处理less
    config.module.rules.push({
      test: /\.less$/,
      use: [
        require.resolve("style-loader"),
        {
          loader: require.resolve("css-loader"),
        },
        require.resolve("less-loader"),
      ],
    });

    // 处理taro
    const jsInclude = config.module.rules[1].include;
    config.module.rules[1].include = (...args) => {
      const p = args[0];
      return p.match("node_modules\\\\@tarojs\\\\taro-h5\\\\src\\\\api") ||
        p.match("node_modules\\\\taro-ui")
        ? true
        : jsInclude(...args);
    };

    return {
      ...config,
      resolve: {
        ...config.resolve,
        alias: {
          ...config.alias,
          ["@tarojs/components$"]: "@tarojs/components/dist-h5/react",
        },
        mainFields: ["main:h5", "browser", "module", "main"],
      },
      plugins: [
        ...config.plugins,
        new webpack.DefinePlugin({
          // ENABLE_ADJACENT_HTML: JSON.stringify(false),
          ENABLE_CLONE_NODE: JSON.stringify(false),
          ENABLE_INNER_HTML: JSON.stringify(false),
          ENABLE_SIZE_APIS: JSON.stringify(false),
          ENABLE_TEMPLATE_CONTENT: JSON.stringify(false),
        }),
      ],
    };
  },
};
